<?php

namespace SDXApi\Abstracts;


/**
 * Class HomeTemplateAbstract
 * @package SDXApi\Service
 */
abstract class TemplateAbstract
{

	/**
	 * 处理专题模块
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return mixed
	 */
	abstract public function handle($template = array(), $userId = 0, $wid = 8);

	/**
	 * 缓存数据
	 * Created by Jerry
	 * @param string $name
	 * @param array $data
	 * @param int $time
	 * @return mixed
	 */
	protected function cacheData($name = '', $data = array(), $time = 3600)
	{
		return S($name, $data, $time);
	}

	/**
	 * 获取缓存
	 * Created by Jerry
	 * @param $name
	 * @return mixed
	 */
	protected function getCache($name)
	{
		return S($name);
	}


	/**
	 * 获取会员等级
	 * Created by Jerry
	 * @param int $userId
	 * @return int
	 */
	protected function getUserRank($userId = 0)
	{
		$rank = D('Users')->get_user_rank($userId);

		if (empty($rank)) {
			return -1;
		}

		return $rank['user_rank'];
	}

	/**
	 * 处理oss地址
	 * Created by Jerry
	 * @param $url
	 * @return mixed
	 */
	static protected function checkImageUrlReplace($url)
	{

		$urlPos = strpos($url, '//oss.shidexian.com');
		if ($urlPos !== false) {
			$url = str_replace('//oss.', 'https://image.', $url);
		}

		return $url;
	}

	/**
	 * 数组排序
	 * Created by Jerry
	 * @param $data
	 * @param string $sortKey
	 * @return mixed
	 */
	static protected function checkDataSort($data, $sortKey = 'sort')
	{
		$sort = array();
		foreach ($data as $key => $value) {
			$sort[] = $value[$sortKey];
		}

		array_multisort($sort, SORT_DESC, $data);

		return $data;
	}

    /**
     * 获取请求来源
     * Created by Jerry
     * @return int
     */
    protected function sourceApp()
    {
        $is_app = I('post.is_app', 0, 'int');
        $is_ios = I('post.is_ios', 0, 'int');
        $is_h5 = I('post.is_h5', 0, 'int');

//		安卓
        if ($is_app && ($is_ios == 0) && ($is_h5 == 0)) {
            return 1;
        }

//		苹果
        if ($is_ios && ($is_app == 0) && ($is_h5 == 0)) {
            return 2;
        }

//		h5
        if ($is_h5 && ($is_ios == 0) && ($is_app == 0)) {
            return 3;
        }

        return 0;
    }
}